package linuxbox.resource.taglib;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import java.util.*;

import linuxbox.resource.logic.*;

/**
 * This class list the resources in the SELECT tag.
 */
public class ResourceList extends TagSupport
{
  protected boolean allowNull = false;
  protected int selected = -1;

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      if ( allowNull )
      {
        out.print("<option value=-1");
        if ( selected == -1 )
          out.print(" selected ");
        out.print(">All resources</option>");
      }
      BoxResource home = BoxResource.getInstance();
      Iterator it = home.getResources("name", true, 0, -1);
      while (it.hasNext())
      {
        BoxResourceEntry entry = (BoxResourceEntry) it.next();
        int id = entry.getID();
        out.print("<option value=\""+id+"\"");
        if ( id == selected)
          out.print(" selected ");
        out.print(">"+entry.getName()+"</option>");
      }
      out.flush();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setAllownull(String aValue)
  {
    allowNull = new Boolean(aValue).booleanValue();
  }

  public void setSelected(int aValue)
  {
    selected = aValue;
  }
}
